package com.ffcs.cn.action.map.addtracking;

import com.supermap.data.*;
import com.supermap.desktop.controls.ui.UICommonToolkit;
import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.utilities.ComponentFactory;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IForm;
import com.supermap.desktop.core.Interface.IFormMap;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.mapping.Map;
import com.supermap.ui.MapControl;
import com.supermap.ui.TrackMode;

import javax.swing.*;
import java.awt.*;

/**
 * 给地图中间添加点线面对象，可以通过GeoStyle来改变对象风格
 *
  */
public class DialogAddTrackingGeo extends SmDialog {
	private JLabel labelType = new JLabel("对象类型：");
	private JComboBox<String> comboBoxType = new JComboBox<>();


	private JButton buttonClear = new JButton("清除");
	private JButton buttonOK = new JButton("添加");
	private JButton buttonClose = new JButton("关闭");

	public DialogAddTrackingGeo() {
		super(((JFrame) Application.getActiveApplication().getMainFrame()), false);
		initComponents();
		initLayouts();
		initListeners();
		initResources();
	}

	private void initComponents() {
		setTitle("添加临时对象");
		this.setSize(new Dimension(300, 140));
		comboBoxType.addItem("点对象");
		comboBoxType.addItem("线对象");
		comboBoxType.addItem("面对象");
	}

	private void initLayouts() {
		setLayout(new GridBagLayout());
		add(labelType, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.NONE).setWeight(0, 0)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0));
		add(comboBoxType, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setAnchor(GridBagConstraints.EAST).setFill(GridBagConstraints.HORIZONTAL).setWeight(1, 0)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP));

		add(ComponentFactory.createButtonPanel(new JComponent[]{buttonClear}, buttonOK, buttonClose), new GridBagConstraintsHelper(0, 99, 2, 1)
				.setAnchor(GridBagConstraints.SOUTH).setFill(GridBagConstraints.NONE).setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP));

	}

	private void initListeners() {
		buttonOK.addActionListener(e -> {
			IForm activeForm = Application.getActiveApplication().getActiveForm();
			if (!(activeForm instanceof IFormMap)) {
				UICommonToolkit.showMessageDialog("请先打开一个地图窗口。");
				return;
			}

			// 拿到地图对象
			MapControl mapControl = ((IFormMap) activeForm).getMapControl();
			Map map = mapControl.getMap();

			String selectedItem = (String) comboBoxType.getSelectedItem();
			Geometry geometry = null;
			Point2D center = map.getCenter();
			if (selectedItem.contains("点")) {
				geometry = new GeoPoint(center.x, center.y);
			} else if (selectedItem.contains("线")) {
				Point2Ds point2Ds = new Point2Ds();
				point2Ds.add(new Point2D(center.x - 20, center.y - 20));
				point2Ds.add(new Point2D(center.x + 20, center.y + 20));
				geometry = new GeoLine(point2Ds);
			} else if (selectedItem.contains("面")) {
				Point2Ds point2Ds = new Point2Ds();
				point2Ds.add(new Point2D(center.x - 20, center.y - 20));
				point2Ds.add(new Point2D(center.x - 20, center.y + 20));
				point2Ds.add(new Point2D(center.x + 20, center.y + 20));
				point2Ds.add(new Point2D(center.x + 20, center.y - 20));
				point2Ds.add(new Point2D(center.x - 20, center.y - 20));
				geometry = new GeoRegion(point2Ds);
			}
			// geometry一般是从Recordset直接获取的：
//			Recordset recordset = null;
//			recordset.getGeometry();

			// 设置对象风格
			GeoStyle geoStyle = new GeoStyle();
			geoStyle.setLineColor(Color.red);
			geoStyle.setMarkerSize(new Size2D(5, 5));
			mapControl.setTrackMode(TrackMode.TRACK);
			map.getTrackingLayer().add(geometry, "tag");
			// 刷新跟踪层
			map.refreshTrackingLayer();
			mapControl.setTrackMode(TrackMode.EDIT);


		});
		buttonClose.addActionListener(e -> DialogAddTrackingGeo.this.dispose());
		buttonClear.addActionListener(e -> {
			// 移除所有跟踪层对象
			((IFormMap) Application.getActiveApplication().getActiveForm()).getMapControl().getMap().getTrackingLayer().clear();

		});
	}

	private void initResources() {

	}
}
